package com.wframe.project.former.dataservices.marketing;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.frame.database.JBaseService;
import com.frame.database.JServiceFactory;
import com.frame.database.JdataBean;
import com.frame.database.PageResult;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/6/14.
 */
public class FillReductionService extends JBaseService<FillReductionDao> {
    public static FillReductionService getIntance() {
        return JServiceFactory.createJServiceProxy(FillReductionService.class);
    }

    public PageResult queryFillReduction(JdataBean parms, int size, int page) throws Exception {
        return getDao().queryFillReduction(parms, size, page);
    }

    public JdataBean moneyoffById(String id) throws Exception {
        List<JdataBean> list = getDao().moneyoffById(id);
        List<Map> datamap = new ArrayList<>();
        for (JdataBean jdataBean : list) {
            datamap.add(jdataBean.getDataMap());
        }
        List<JdataBean> list1 =getDao().moneyoffByIdRata(id);
        List<Map> datamap2 = new ArrayList<>();
        for (JdataBean jdataBean1 : list1) {
            datamap2.add(jdataBean1.getDataMap());
        }
        JdataBean data = getDao().promotByid(id);
        data.put("items", datamap);
        data.put("itemsrate",datamap2);
        return data;
    }

    public void addMoneyOff(String promotionid, String arr) throws Exception {
        getDao().delectMontyOff(promotionid);
        JSONArray json = JSONArray.parseArray(arr);
        for (int i = 0; i < json.size(); i++) {
          JdataBean bean = new JdataBean();
           JSONObject jsonObject = json.getJSONObject(i);
                jsonObject.put("promotionid",promotionid);
               getDao().addMoneyOff(jsonObject);
        }

    }

    public PageResult queryMoneyProduct(JdataBean parms, int size, int pageno) throws Exception {
        return getDao().queryMoneyProduct(parms, size, pageno);
    }

    public List<JdataBean> canCreate() throws Exception {
        return getDao().canCreate();
    }

    public List<JdataBean> canCreate(String proClass) throws Exception {
        List<JdataBean> promotionBean = getDao().canCreate(proClass);
        List<JdataBean> productToList = queryProductToclass(proClass);
        if (promotionBean.size() > 0) {
            return promotionBean;
        }
        return productToList;
    }

    public List<JdataBean> canCreate(List<String> arr) throws Exception {
        return getDao().canCreate(arr);
    }

    public void promotProduct(String promotionid, List<String> ids) throws Exception {
        getDao().delectPromotionProducr(promotionid);
        JdataBean jdataBean = new JdataBean();
        for (String id : ids) {
            jdataBean.put("promotionid", promotionid);
            jdataBean.put("productid", id);
            getDao().addPromotionProduct(jdataBean);
        }
    }

    public List<JdataBean> queryProductToclass (String classid) throws Exception {
        return getDao().queryProductToclass(classid);
    }
}
